package skin;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import skin.SkinAnalysisOptimizedcopy;

import java.io.File;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*")  // 允许跨域请求
public class SkinAnalysisController {
    
    @PostMapping("/analyze")
    public ResponseEntity<String> analyzeSkin(@RequestParam("file") MultipartFile file) {
        try {
            // 检查文件类型
            String contentType = file.getContentType();
            if (!"image/jpeg".equals(contentType)) {
                return ResponseEntity.badRequest().body("只支持JPG/JPEG格式的图片");
            }

            // 创建临时文件
            File tempFile = File.createTempFile("upload_", ".jpg");
            file.transferTo(tempFile);
            
            // 调用分析方法
            String result = SkinAnalysisOptimizedcopy.analyzeSkin(tempFile);
            String analysis = SkinAnalysisOptimizedcopy.printOptimizedResult(result);
            
            // 删除临时文件
            tempFile.delete();
            
            return ResponseEntity.ok()
                .header("Content-Type", "text/plain;charset=UTF-8")
                .body(analysis);
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                .body("分析失败: " + e.getMessage());
        }
    }
} 